//清除优惠券数据 2017.7.17 GJC
sessionStorage.removeItem("couponData");
//选取动画框；
$('.buy_type  li').on('click', function () {
    var $after = '<div class="after"></div>';
    var $before = '<div class="before"></div>';
    var $index = $(this).index();
    var $parent = $(this).parent('.flex_row');;
    if ($parent.find('li').eq($index).find('div').hasClass('after') || $parent.find('li').eq($index).find('div').hasClass('before')) {
        return false
    } else {
        $('.buy_type li').children('.before').remove();
        $('.buy_type li').children('.after').remove(); //重置样式
        $parent.find('li').eq($index).append($after);
        $parent.find('li').eq($index).prepend($before);
        $parent.find('li').eq($index).children('.before').animate({
            "width": "75%",
            "height": "70%"
        }, 800)
        $parent.find('li').eq($index).children('.after').animate({
            "height": "70%",
            "width": "75%"
        }, 800)
    }
})
var itemName = "万能排队";
//排队信息跟着变
$(".buy_type li").on('click', function () {
    $(".buy_type li span").css('visibility', 'hidden');
    $(this).children("span").css("visibility", "visible");
    var buyType = $(this).children("i").attr("class").split(" ")[1];
    var buyName = $(this).find(".name").html();
    itemName = buyName;
    var str = "";
    str += "<i class='iconfont " + buyType + "'></i><div class='" + name + "'>" + buyName + "</div>"
    $(".buy_change").html(str);

})


// //时间选择器控件  iscroll5 + jq
var myScroll;
var today = new Date();
var nowYear = today.getFullYear();
var nowMonth = today.getMonth() + 1;
var nowDay = today.getDate();
var nowHour = today.getHours();
var nowMinute = today.getMinutes();
// var nowTime = nowYear + "-" + nowMonth + "-" + nowDay + " " + (nowHour < 10 ? '0' : '') + nowHour + ':' + (nowMinute < 10 ? '0' : '') + nowMinute;
var nowTime="";
//分钟范围
if (nowMinute > 55) {
    nowHour = nowHour + 1;
    nowMinute = 0;
}
//小时范围
if (nowHour > 23) {
    nowHour = 0;
    nowMinute = 0;
};
var reMin = Math.ceil(nowMinute / 5),
    reHour = nowHour,
    reDay = 0;
var dayIndex = 0,
    hourIndex = nowHour;
var minIndex = Math.ceil(nowMinute / 5);
//字符串
var orderDay = "今天",
    orderHour = (nowHour < 10 ? '0' : '') + nowHour,
    orderMinute = (minIndex == 0 ? '0' : '') + minIndex * 5,
    timeStr = "";
function getNowTime(count) {
    var date = new Date();
    date.setDate(date.getDate() + count)
    var thisYear = date.getFullYear();
    var thisMonth = date.getMonth() + 1;
    var thisDay = date.getDate();
    return nowTime = thisYear + "-" + thisMonth + "-" + thisDay + " " + orderHour + ':' + orderMinute;
}
//取消按钮文本
var cancelTxt = "立即排队";
$('.time_box .cancel').html(cancelTxt);
var selectTimeOn = false;//没有选择排队时间   代排队计价功能关；
$("#select_time").on('click', function () {
    //刷新时间
    $('.mask').css('display', 'block'); //遮罩层出现	
    $('.time_box').css('display', 'block');
    var dayScroll = new IScroll('#time_day', {
        scrollY: true,
        listenY: true,
        tap: true,
        momentum: true,
        preventDefault: false,
        snap: 'li'
    });
    var hourScroll = new IScroll('#time_hour', {
        scrollY: true,
        listenY: true,
        momentum: true,
        preventDefault: false,
        tap: true,
        snap: 'li'
    });
    var minuteScroll = new IScroll('#time_minute', {
        scrollY: true,
        listenY: true,
        momentum: true,
        preventDefault: false,
        tap: true,
        snap: 'li'
    });
    //根据当前时间重置时间列表
    var liHeight = parseInt($("#time_hour li").css("height"));

    function rewriteList() {
        //无问题；
        //判断是否为当前时间；列表的第一个	
        dayIndex = 0;
        hourIndex = nowHour;
        minIndex = reMin;
        $("#time_day li").removeClass('li_select').eq(0).addClass('li_select');
        dayScroll.refresh();
        dayScroll.goToPage(0, 0);
        $('#time_hour li').css("display", "block").slice(0, reHour).css("display", "none");
        $('#time_hour li').removeClass('li_select').eq(reHour).addClass('li_select');
        hourScroll.refresh();
        hourScroll.goToPage(0, 0);
        $('#time_minute li').css("display", "block").slice(0, reMin).css("display", "none");
        $("#time_minute li").removeClass('li_select').eq(reMin).addClass('li_select');
        minuteScroll.refresh();
        minuteScroll.goToPage(0, 0);
        orderDay = "今天";
        orderHour = (nowHour < 10 ? "0" : "") + nowHour;
        orderMinute = (reMin == 0 ? "0" : "") + reMin * 5;
    }
    rewriteList(); //执行重置
    //日期滑动结束函数
    dayScroll.on('scrollEnd', function (e) {
        dayHeight = Math.abs(this.y);
        var maxLen = $("#time_day li").length - 1;
        dayIndex = Math.ceil(dayHeight / liHeight);
        if (dayIndex >= maxLen) {
            console.log("dayIndex>maxLen" + dayIndex);
            dayIndex = maxLen - 1;

        }
        var isToday = (dayIndex <= 0);
        var isNowHour = (hourIndex == nowHour);
        $("#time_day li").removeClass('li_select').eq(dayIndex).addClass('li_select');
        if (isToday) {
            $("#time_hour li").css("display", "block").slice(0, reHour).css("display", "none");
            $('#time_hour li').removeClass('li_select').eq(reHour).addClass('li_select');
            hourScroll.refresh();
            hourScroll.goToPage(0, reHour);
            minIndex = reMin;
            $("#time_minute li").css("display", "block").slice(0, reMin).css("display", "none");
            $('#time_minute li').removeClass('li_select').eq(reMin).addClass('li_select');
            minuteScroll.refresh();
            minuteScroll.goToPage(0, reMin);
        } else {
            $('#time_hour li').css("display", "block").removeClass('li_select').eq(0).addClass('li_select');
            hourScroll.refresh();
            hourScroll.goToPage(0, 0);
            $('#time_minute li').css("display", "block").removeClass('li_select').eq(0).addClass('li_select');
            hourIndex = 0;
            minIndex = 0;
            minuteScroll.goToPage(0, 0);
        }
        orderDay = $("#time_day .li_select").html(); //当前天数字符串；
        orderHour = $("#time_hour .li_select").html();
        orderMinute = $("#time_minute .li_select").html();
        // event.stopPropagation();
        // event.preventDefault();
    })
    hourScroll.on('scrollEnd', function () {
        //顺序不能变
        var hourHeight = Math.abs(this.y);
        var maxLen = $("#time_hour li").length - 1;
        hourIndex = Math.ceil(hourHeight / liHeight);
        var isToday = (dayIndex <= 0);
        if (isToday) {
            hourIndex = Math.ceil(hourHeight / liHeight) + nowHour;
        } else {
            hourIndex = Math.ceil(hourHeight / liHeight);
        }

        if (hourIndex >= maxLen) {
            hourIndex = maxLen - 1;
        }
        $("#time_hour li").removeClass('li_select').eq(hourIndex).addClass('li_select');
        hourScroll.refresh();
        hourScroll.goToPage(0, hourIndex);
        var isNowHour = (hourIndex == nowHour);
        if (isToday && isNowHour) {
            $('#time_minute li').css("display", "block").slice(0, reMin).css("display", "none");
            $('#time_minute li').removeClass('li_select').eq(reMin).addClass('li_select');
            minuteScroll.refresh();
            minuteScroll.goToPage(0, 0);
        } else {
            $('#time_minute li').css("display", "block").removeClass('li_select').eq(0).addClass('li_select');
            minuteScroll.refresh();
            minuteScroll.goToPage(0, 0);
        }
        orderDay = $("#time_day .li_select").html(); //当前天数字符串；
        orderHour = $("#time_hour .li_select").html();
        orderMinute = $("#time_minute .li_select").html();
    })
    //分钟滑动结束函数
    minuteScroll.on('scrollEnd', function () {
        var minuteHeight = Math.abs(this.y);
        minIndex = Math.ceil(minuteHeight / liHeight);
        var isToday = (dayIndex <= 0);
        var isNowHour = (hourIndex == nowHour);
        if (isToday && isNowHour) {
            minIndex = Math.ceil(minuteHeight / liHeight) + Math.ceil(nowMinute / 5);
        } else {
            minIndex = Math.ceil(minuteHeight / liHeight);
        }
        $("#time_minute li").removeClass('li_select').eq(minIndex).addClass('li_select');
        orderDay = $("#time_day .li_select").html(); //当前天数字符串；
        orderHour = $("#time_hour .li_select").html();
        orderMinute = $("#time_minute .li_select").html();

    })
    //day列表点击效果
    $('#time_day li').off("tap").on('tap', tapDay);

    function tapDay() {
        var maxLen = $("#time_day li").length - 1;
        dayIndex = $(this).index();
        if (dayIndex == maxLen) {
            dayIndex = maxLen - 1;
        }
        $('#time_day li').removeClass('li_select').eq(dayIndex).addClass('li_select');
        dayScroll.goToPage(0, dayIndex);
    }
    //hour列表点击效果
    $('#time_hour li').off("tap").on('tap', tapHour);

    function tapHour() {
        var maxLen = $("#time_hour li").length - 1;
        hourIndex = $(this).index();
        if (hourIndex == maxLen) {
            hourIndex = maxLen - 1;
        }
        $('#time_hour li').removeClass('li_select').eq(hourIndex).addClass('li_select');
        hourScroll.goToPage(0, hourIndex);
    }
    //minute列表点击效果
    $('#time_minute li').off("tap").on('tap', tapMinute);

    function tapMinute() {
        var maxLen = $("#time_minute li").length - 1;
        minIndex = $(this).index();
        if (minIndex == maxLen) {
            minIndex = maxLen - 1;
        }
        $('#time_minute li').removeClass('li_select').eq(minIndex).addClass('li_select');
        minuteScroll.goToPage(0, minIndex);
    }
    // 解决卡顿问题
    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
    }, false);
    var refreshScroll = function refreshMyScroll() {
        dayScroll.refresh();
        hourScroll.refresh();
        minuteScroll.refresh();
    }
    setTimeout(refreshScroll, 0);
    //销毁myScroll
    function destroyMyScroll() {
        clearTimeout(refreshScroll);
        hourScroll.destroy();
        hourScroll = null;
        minuteScroll.destroy();
        minuteScroll = null;
        dayScroll.destroy();
        dayScroll = null;
    }
    //事件嵌套事件会叠加 需要解除绑定事件 on对应off bind对应unbind
    $(".time_box .confirm").off('click').on("click", function (e) {
        $(".mask").css("display", "none");
        $(".time_box").css('display', "none");
        timeStr = orderDay + "  " + orderHour + ":" + orderMinute;
        $("#select_time").html(timeStr);
        $(".time_box").unbind("click");
        if (orderDay == "今天") {
            getNowTime(0);
            // console.log(nowTime);
        } else if (orderDay == "明天") {
            getNowTime(1);
            // console.log(nowTime);
        } else if (orderDay == "后天") {
            getNowTime(2);
            // console.log(nowTime);
        };
        console.log(timeStr);
        selectTimeOn = true;//代排队计价功能开
        getOrderMoney();//获取排队帮忙价格
        destroyMyScroll();
        event.stopPropagation();

    })
    $(".time_box .cancel").off('click').on("click", function () {
        $(".mask").css("display", "none");
        $(".time_box").css('display', "none");
        $("#select_time").html(cancelTxt);
        selectTimeOn = true;//代排队计价功能开
        getOrderMoney();//获取排队帮忙价格
        destroyMyScroll();
    })
    // event.stopPropagation();
});


//排队时长
var queueHour = 0, queueToHour = 0;
var queueMinute = 30, queueToMinute = 1;
var queueTimeStr = "", queueTime = 30;
$('#queue_time').click(function () {
    $('.mask').css('display', 'block');
    $('.queue_time_box').css('display', 'block');
    var tipHourScroll = new IScroll('#queue_time_hour', {
        scrollY: true,
        listenY: true,
        tap: true,
        momentum: false,
        snap: 'li'
    })
    tipHourScroll.goToPage(0, queueToHour);
    tipHourScroll.refresh();
    var tipMinuteScroll = new IScroll('#queue_time_minute', {
        scrollY: true,
        listenY: true,
        tap: true,
        momentum: false,
        snap: 'li'
    });
    var liHeight = parseInt($('.queue_time_body li').css("height"));
    tipMinuteScroll.goToPage(0, queueToMinute);
    tipHourScroll.on('scrollEnd', function () {
        var tipHourHeight = Math.abs(this.y);
        tipHourIndex = Math.ceil(tipHourHeight / liHeight);
        queueToHour = tipHourIndex;
        $("#queue_time_hour li").removeClass('li_select').eq(tipHourIndex).addClass('li_select');
        queueHour = parseInt($("#queue_time_hour .li_select span").html());
    });

    tipMinuteScroll.on('scrollEnd', function () {
        var tipMinuteHeight = Math.abs(this.y);
        tipMinuteIndex = Math.ceil(tipMinuteHeight / liHeight);
        queueToMinute = tipMinuteIndex;
        $("#queue_time_minute li").removeClass('li_select').eq(tipMinuteIndex).addClass('li_select');
        queueMinute = parseInt($("#queue_time_minute .li_select span").html());
    })
    //tap点击事件；
    $('#queue_time_hour li').off("tap").on("tap", function () {
        var maxLen = $("#queue_time_hour li").length - 1;
        queueToHour = $(this).index();
        if (queueToHour == maxLen) {
            queueToHour = maxLen - 1;
        }
        $('#queue_time_hour li').removeClass('li_select').eq(queueToHour).addClass('li_select');
        tipHourScroll.goToPage(0, queueToHour);
    })
    $('#queue_time_minute li').off("tap").on("tap", function () {
        var maxLen = $("#queue_time_minute li").length - 1;
        queueToMinute = $(this).index();
        if (queueToMinute == maxLen) {
            queueToMinute = maxLen - 1;
        }
        $('#queue_time_minute li').removeClass('li_select').eq(queueToMinute).addClass('li_select');
        tipMinuteScroll.goToPage(0, queueToMinute);
    })
    function destroyQueueMyScroll() {
        tipHourScroll.destroy();
        tipHourScroll = null;
        tipMinuteScroll.destroy();
        tipMinuteScroll = null;
    }
    $('.queue_time_box .confirm').off('click').on("click", function () {
        $('.mask').css('display', 'none');
        $('.queue_time_box').css('display', 'none');
        if (queueHour == 0) {
            queueTimeStr = queueMinute + "分钟";
            queueTime = queueMinute;
        } else if (queueMinute == 0) {
            queueTimeStr = queueHour + "小时";
            queueTime = queueHour * 60;
        } else if (queueMinute == 0 && queueHour == 0) {
            queueTimeStr = "默认30分钟";
            queueTime = 30;
        } else {
            queueTimeStr = queueHour + "小时" + queueMinute + "分钟";
            queueTime = queueHour * 60 + queueMinute;
        }
        console.log(queueTime);
        $("#queue_time").html(queueTimeStr);
        getOrderMoney();
        destroyQueueMyScroll();
    })
    $('.queue_time_box .cancel').off('click').on("click", function () {
        $('.mask').css('display', 'none');
        $('.queue_time_box').css('display', 'none');
        queueMinute = 30;
        queueTime = 30;
        queueTimeStr = queueMinute + "分钟";
        $("#queue_time").html(queueTimeStr);
        getOrderMoney();
        destroyQueueMyScroll();
    })
})
//获得订单价格
var orderMoney = 0,
    moneyDetail = "";

function getOrderMoney() {
    $.ajax({
        type: "get",
        url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_OrderMoneyByMin",
        data: {
            "Minutes": queueTime,
        },
        dataType: "json",
        success: function (data) {
            if (data.msgcode == "1") {
                // console.log(data);
                if (selectTimeOn == true) { //排队计价功能
                    orderMoney = data.data.OrderMoney;
                    moneyDetail = data.data.moneyDetail;
                    $(".money_detail").html(moneyDetail);
                    $(".form_submit").html(data.data.OrderMoney + "元，去支付");
                }
            }
        },
        error:function(){
            $.alert("计价接口错误");
        }
    })
};
//获取排队类型信息
$.ajax({
    type: "get",
    url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_WuPinHelper",
    data: {
        "Types": "3",
    },
    dataType: "json",
    success: function (data) {
        if (data.msgcode == "1") {
            // console.log(data);
        }
    }
})
//商品信息交互优化 2017-6-20 Desc
var Desc = sessionStorage.getItem("Desc");
$("#product_mes").val(Desc);
$("#product_mes").blur(function () {
    sessionStorage.setItem("Desc", $("#product_mes").val());
})
//获取用户信息对象
var userInfo = localStorage.getItem("userInfo");
var userInfoObj = JSON.parse(userInfo);
var userAddStr = sessionStorage.getItem("userAddMes");
var userAddObj = {},
    QuHuoAddress = "";
if (userAddStr) {
    userAddObj = JSON.parse(userAddStr);
    QuHuoAddress = userAddObj.QuHuoAddress;
    if (QuHuoAddress != "") {
        $("#detail_address").html(QuHuoAddress);
    };
    $("#user_tel").val(userAddObj.Quhuo_Tel);
}
$(".form_submit").on('click', function () {
    if (QuHuoAddress == "") {
        $.alert("请输入地址信息")
        return false;
    } else if (selectTimeOn == false) {
        $.alert("请选择排队时间")
        return false;
    } else if ($("#user_tel").val() == "") {
        $.alert("请输入联系号码")
        return false;
    } else {
        Desc = $("#product_mes").val();
        var UserId = userInfoObj.data.UserId;
        var orderData = {
            "Near": "0",
            "Baowenxiang": "0",
            "QuHuoAddress": userAddObj.QuHuoAddress,
            "Quhuo_Lng": userAddObj.Quhuo_Lng,
            "Quhuo_Lat": userAddObj.Quhuo_Lat,
            "Quhuo_Person": userAddObj.Quhuo_Person,
            "Quhuo_Tel": userAddObj.Quhuo_Tel,
            "BangMangId": "18",
            "BangName": itemName,
            "ZhiPrice": "0",
            "WuPin_Money": "0",
            "Desc": Desc,
            "YiSui": "0",
            "Quhuo_Date": nowTime,
            "YouId": "0",
            "YouMoney": "0",
            "CityId": "155",
            "UserId": UserId,
            "UserTel": userInfoObj.data.Phone,
            "Types": "5",
            "ShiChang": queueTime,
            "IsTouBao": "0",
            "TouMoney": "0",
            "PeiMoney": "0",
            "orderMoney":orderMoney
        }
        orderData = JSON.stringify(orderData);
        sessionStorage.setItem("orderData", orderData);
        window.location.href = "payment_queue.html";
    }
})






